package com.ymt.bpm.web.rest.platform.attach;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.service.platform.attach.AttachmentService;
import com.ymt.bpm.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Author Johnny
 */
@RestController
@RequestMapping("/api/v1/platform/attach")
public class AttachmentResource {

    /**
     * Logger
     */
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AttachmentService as;

    @GetMapping("/initconf")
    public JSONObject initConf(HttpServletRequest request) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        JSONObject rtn = as.initConf(tenantId);
        rtn.put(Const.LOGINNAME, loginName);
        return rtn;
    }

    @GetMapping("/signatureUp")
    public JSONObject signatureUp(HttpServletResponse response,
            @RequestParam("timezoneOffset")int timezoneOffset) {
        try {
            //response.setHeader("Access-Control-Allow-Origin", "*");
            JSONObject rtn = as.signatureUp(timezoneOffset);
            return rtn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/signatureDown")
    public JSONObject signatureDown(HttpServletResponse response,
                                  @RequestParam("key")String key,
                                  @RequestParam("timezoneOffset")int timezoneOffset) {
        JSONObject rtn = as.signatureDown(key, timezoneOffset);
        return rtn;
    }

    @PostMapping("/saveAttach")
    public JSONObject saveAttach(HttpServletRequest request,
                                @RequestParam("taskInfo") String taskInfo,
                                @RequestParam("files") String files,
                                @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        return as.saveToDbFiles(tenantId, loginName, displayName, taskInfo, files, timezoneOffset);
    }

    @DeleteMapping("/delAttach")
    public JSONObject saveAttach(HttpServletRequest request,
                                @RequestParam("piId") String piId,
                                @RequestParam("attachmentId") String attachmentId,
                                @RequestParam(value="versionId", required = false) String versionId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        return as.delDbFile(tenantId, piId, attachmentId, versionId);
    }

    @GetMapping("/listAttachs")
    public JSONObject listFiles(HttpServletRequest request,
                               @RequestParam("piId")String piId,
                               @RequestParam(value = "tiId", required = false)String tiId,
                               @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        return as.listDbFiles(tenantId, piId, tiId, timezoneOffset);
    }

    /*@GetMapping("/getVersions")
    public JSONArray getVersions(HttpServletRequest request,
                               @RequestParam("attachmentId") String attachmentId,
                               @RequestParam("timezoneOffset")int timezoneOffset) {
        String tenantId = userDetail.get(Const.TENANT_ID);
        return as.getVersions(tenantId, attachmentId, timezoneOffset);
    }*/

}
